const API = require('../../../../utils/api')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        gardenId: '',
        records: [],
        pageNum: 1,
        reachbottom: false,
        parameter: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            gardenId: options.id
        })
        this.get_park_work_records()
    },
    searchChange(event) {
        this.data.parameter = event.detail.value
     },
    searchConfirm() {
        this.data.pageNum = 1
        this.get_park_work_records()
    },

    onReachBottom: function () {
        if (this.data.reachbottom) {
            let pageNum = this.data.pageNum
            pageNum++
            this.setData({
                pageNum
            })
            this.get_park_work_records()
        }
    },

    /**
     * 接口 - 看房工单列表
     */
    get_park_work_records: function () {
        let that = this
        API.get_park_work_records({
            parameter: this.data.parameter,
            gardenId: that.data.gardenId,
            pageSize: 10,
            pageNum: that.data.pageNum
        }).then(res => {
            if (res.code == 200) {
                let rows = res.rows.map(ele => {
                    ele.createTime = ele.createTime ? ele.createTime.split(' ')[0] : ''
                    ele.show = true
                    return ele
                })
                console.log('列表', rows)
                that.setData({
                    records:  that.data.pageNum == 1 ? rows : [...that.data.records, ...rows]
                })
                if (res.rows.length >= 10) {
                    that.setData({
                        reachbottom: true
                    })
                } else {
                    that.setData({
                        reachbottom: false
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        })
    },

    /**
     * 打开详情
     */
    showDetail: function (e) {
        let index = e.currentTarget.id
        let records = this.data.records
        records[index].show = !records[index].show
        this.setData({
            records
        })
        console.log(records)
    },

    /**
     * 打电话
     */
    callPhone: function (e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.tel,
        })
        // this.setData({
        //     records: []
        // })
        let that = this
        API.update_park_work_record_status({
            id: parseInt(e.currentTarget.id)
        }).then(res => {
            // if (res.code == 200) {
            //     that.get_park_work_records()
            // }
        })
    },

    /**
     * 微信支付
     */
    wxPay: function (e) {
        let item = e.currentTarget.dataset.item
        API.creat_wxpay_code({
            orderNo: item.orderNo,
            userId: item.userId
        }).then(res => {
            if (res.appId && res.nonceStr) {
                wx.requestPayment({
                    timeStamp: res.timeStamp,
                    nonceStr: res.nonceStr,
                    package: res.packAge,
                    signType: 'MD5',
                    paySign: res.paySign,
                    success(re) {
                        console.log(re)
                        if (re.errMsg == 'requestPayment:ok') {
                            wx.showToast({
                                title: '支付成功',
                            })
                        }
                    },
                    fail(re) {
                        console.log(re)
                        if (re.errMsg == 'requestPayment:fail cancel') {
                            wx.showToast({
                                title: '您已取消',
                                icon: 'error'
                            })
                        } else {
                            wx.showToast({
                                title: re.errMsg,
                                icon: 'none'
                            })
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        })
    },
})